﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ComponentModel;

using PermDoc.Common;
using PermDoc.Ctrl.Docs;

namespace PermDoc.Ctrl
{

    public class InfoPanel
    {

    }

    public class InfoPanelData : INotifyPropertyChanged
    {
        #region Members
        public Int64 DocCountTotal_new { get; set; }
        public Int32 DocCountToDay_new { get; set; }
        public Int32 DocCountYesterDay_new { get; set; }
        public Int32 DocCountLastWeek_new { get; set; }
        public Int32 DocCountLastMonth_new { get; set; }
        public Int64 DocCountTotal_edt { get; set; }
        public Int32 DocCountToDay_edt { get; set; }
        public Int32 DocCountYesterDay_edt { get; set; }
        public Int32 DocCountLastWeek_edt { get; set; }
        public Int32 DocCountLastMonth_edt { get; set; }
        
        public DateTime? BeforeCreated { get; set; }
        DateTime Now;
        DateTime YesterDay;
        DateTime dtLastMonday;
        DateTime dtLastLastMonday;
        DateTime dtPrevMonthEnd;
        DateTime dtPrevMonthStart;

        public ulong InvalidateCountCurDay { get; set; }
        public ulong InvalidateCountTotal { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion Members
        #region Constructor
        public InfoPanelData()
        {
            DocCountTotal_new = 0;
            DocCountToDay_new = 0;
            DocCountYesterDay_new = 0;
            DocCountLastWeek_new = 0;
            DocCountLastMonth_new = 0;
            DocCountTotal_edt = 0;
            DocCountToDay_edt = 0;
            DocCountYesterDay_edt = 0;
            DocCountLastWeek_edt = 0;
            DocCountLastMonth_edt = 0;
            BeforeCreated = DateTime.Now;

            InvalidateCountCurDay = 0;
            InvalidateCountTotal = 0;

            UpdateDaysConstsState();
        }
        #endregion
        #region Events
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        #region Common Methods
        public void UpdateDaysConstsState()
        {
            Now = DateTime.Now;
            YesterDay = Now - (new TimeSpan(1, 0, 0, 0));
            dtLastMonday = Now.StartOfWeek(DayOfWeek.Monday) - new TimeSpan(1,0,0,0);
            dtLastMonday = new DateTime(dtLastMonday.Year, dtLastMonday.Month, dtLastMonday.Day, 23, 59, 59, 999);
            dtLastLastMonday = dtLastMonday - (new TimeSpan(6, 0, 0, 0));
            dtLastLastMonday = new DateTime(dtLastLastMonday.Year, dtLastLastMonday.Month, dtLastLastMonday.Day);
            dtPrevMonthEnd = Now.PreviousMonth();
            dtPrevMonthEnd = new DateTime(dtPrevMonthEnd.Year, dtPrevMonthEnd.Month, dtPrevMonthEnd.Day, 23, 59, 59, 999);
            dtPrevMonthStart = new DateTime(dtPrevMonthEnd.Year, dtPrevMonthEnd.Month, 1);
        }
        public void InvalidateControls()
        {
            NotifyPropertyChanged("DocCountTotal_new");
            NotifyPropertyChanged("DocCountToDay_new");
            NotifyPropertyChanged("DocCountYesterDay_new");
            NotifyPropertyChanged("DocCountLastWeek_new");
            NotifyPropertyChanged("DocCountLastMonth_new");
            NotifyPropertyChanged("DocCountTotal_edt");
            NotifyPropertyChanged("DocCountToDay_edt");
            NotifyPropertyChanged("DocCountYesterDay_edt");
            NotifyPropertyChanged("DocCountLastWeek_edt");
            NotifyPropertyChanged("DocCountLastMonth_edt");
            NotifyPropertyChanged("BeforeCreated");
            NotifyPropertyChanged("InvalidateCountCurDay");
            NotifyPropertyChanged("InvalidateCountTotal");
        }
        public void ReMakeStatistics()
        {
            UpdateDaysConstsState();
            DocCountTotal_new = 0;
            DocCountToDay_new = 0;
            DocCountYesterDay_new = 0;
            DocCountLastWeek_new = 0;
            DocCountLastMonth_new = 0;
            DocCountTotal_edt = 0;
            DocCountToDay_edt = 0;
            DocCountYesterDay_edt = 0;
            DocCountLastWeek_edt = 0;
            DocCountLastMonth_edt = 0;
            foreach (Topic o in App.Topics)
            {
                DateTime? dt_new = o.Создан;
                DateTime? dt_edt = o.Изменён;
                NewItem(dt_new, dt_edt);
            }
        }
        public void NewItem(DateTime? dt_new, DateTime? dt_edt)
        {
            if (!((dt_new == null) || (dt_new.Value == null)))
            {
                // To day
                if (Now.Year == dt_new.Value.Year && Now.Month == dt_new.Value.Month && Now.Day == dt_new.Value.Day)
                {
                    DocCountToDay_new++;
                }
                // Yester Day
                if (YesterDay.Year == dt_new.Value.Year && YesterDay.Month == dt_new.Value.Month && YesterDay.Day == dt_new.Value.Day)
                {
                    DocCountYesterDay_new++;
                }
                // Last Week
                if (dt_new > dtLastLastMonday && dt_new <= dtLastMonday)
                {
                    DocCountLastWeek_new++;
                }
                // Last Month
                if (dt_new > dtPrevMonthStart && dt_new <= dtPrevMonthEnd)
                {
                    DocCountLastMonth_new++;
                }
                DocCountTotal_new++;
            }

            if (!((dt_edt == null) || (dt_edt.Value == null)))
            {
                // To day
                if (Now.Year == dt_edt.Value.Year && Now.Month == dt_edt.Value.Month && Now.Day == dt_edt.Value.Day)
                {
                    DocCountToDay_edt++;
                }
                // Yester Day
                if (YesterDay.Year == dt_edt.Value.Year && YesterDay.Month == dt_edt.Value.Month && YesterDay.Day == dt_edt.Value.Day)
                {
                    DocCountYesterDay_edt++;
                }
                // Last Week
                if (dt_edt > dtLastLastMonday && dt_edt <= dtLastMonday)
                {
                    DocCountLastWeek_edt++;
                }
                // Last Month
                if (dt_edt > dtPrevMonthStart && dt_edt <= dtPrevMonthEnd)
                {
                    DocCountLastMonth_edt++;
                }
                DocCountTotal_edt++;
            }
        }
        public void UpdateItem(DateTime? dt_new, DateTime? dt_edt)
        {
        }
        #endregion
    }

}
